package org.scboy.common.dynamic.node.whereNode;

import java.util.List;

import org.scboy.common.annotation.ScriptNode;
import org.scboy.common.dynamic.node.BaseNode;
import org.scboy.utils.entitys.coobi.Model;

import cn.hutool.json.JSONObject;


@ScriptNode("WHERE_OR_ENTITY")
public class WhereOrEntityNode extends BaseNode{

	@Override
	public void create(JSONObject object) {
		
	}

	@Override
	public Object run() {
		/**
		 * 测试日志
		 */
		if(this.script.isTest()) {
			this.script.appendMsg("----->开始执行："+getNodeName()+" 节点ID："+this.getId());
		}
		
		/**
		 * 流出节点
		 */
		BaseNode runTo = getRunTo("to");
		
		Model data=Model.create();

		//加入条件
		List<Model> mergeList=getSourceDatas("params", true, Model.class);
		for (Model mergeData : mergeList) {
			//判断是否是子查询
			if(mergeData.getBoolean("______childQuery")) {
				if(mergeData.getBoolean("queryType")) {
					data.and((Model)mergeData.get("data"));
				}else {
					data.or((Model)mergeData.get("data"));
				}
			}else {
				for (String key : mergeData.keySet()) {
					data.put(key, mergeData.get(key));
				}
			}
		}
		
		this.setData("data", data);

		this.setData("where", Model.create().add("______childQuery", true).add("queryType", false).add("data", data));
		/**
		 * 测试日志
		 */
		if(this.script.isTest()) {
			this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
			this.script.appendMsg("执行下个节点："+runTo.getNodeName()+" 节点ID："+runTo.getId());
		}
		return runTo.run();
	}
	
}
